package com.delicaterock.mpw;

import com.google.zxing.client.android.CaptureActivity;
import com.google.zxing.client.android.Intents;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainScreen extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btnPassword = (Button) findViewById(R.id.btn_password);
        btnPassword.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditText txtPassword = (EditText)findViewById(R.id.txt_password);
				SimpleDBHelper.password = txtPassword.getText().toString();
				if (SimpleDBHelper.password.length()==0) {
					showEmptyPasswordDialog();
				} else {
					LinearLayout gPassword = (LinearLayout)findViewById(R.id.group_password);
					gPassword.setVisibility(View.INVISIBLE);
					LinearLayout gBtns = (LinearLayout)findViewById(R.id.group_btns);
					gBtns.setVisibility(View.VISIBLE);
				}
			}});        	

        Button btnHelp = (Button) findViewById(R.id.btn_help);
        btnHelp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {				
				Intent it = new Intent(Intent.ACTION_VIEW);
				it.setData(Uri.parse("http://code.google.com/p/mobile-password-wallet/wiki/BasicUsage"));
				startActivity(it);
			}});        	
        
        Button btnWallet = (Button) findViewById(R.id.btn_wallet);
        btnWallet.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent it = new Intent(MainScreen.this, WalletScreen.class);
				startActivity(it);
			}});
        
        Button btnScan = (Button) findViewById(R.id.btn_scan);
        btnScan.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent it = new Intent(MainScreen.this, CaptureActivity.class);
				it.putExtra(Intents.Scan.SCAN_FORMATS, Intents.Scan.QR_CODE_MODE);
				startActivityForResult(it, 0);
//				String barcode = "mpw://127.0.0.1,9.123.229.75:4444@1320223086765/PowerPoint_";
//				Intent it = new Intent(MainScreen.this, PPTScreen.class);
//				it.putExtra("url", barcode);
//				startActivity(it);				
			}});        
    }
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent result) {
		if (requestCode==0) {
			if (resultCode == RESULT_OK) {
				String barcode = result.getStringExtra("barcode");
				Log.i("...barcode...", barcode);
				Intent it = null;
				if (barcode.contains("PowerPoint_")) {
					it = new Intent(MainScreen.this, PPTScreen.class);
				} else {				
					it = new Intent(MainScreen.this, ConnectionScreen.class);
				}
				it.putExtra("url", barcode);
				startActivity(it);
			}
		}
	} 
	
	private void showExitDialog() {  
		AlertDialog.Builder builder = new AlertDialog.Builder(MainScreen.this);  
		builder.setIcon(R.drawable.icon);  
		builder.setTitle("Information");  
		builder.setMessage("Exit?");  
		builder.setPositiveButton("Yes",  
			new DialogInterface.OnClickListener() {  
				public void onClick(DialogInterface dialog, int whichButton) {
					finish();
				}
			});  
		builder.setNegativeButton("No",  
			new DialogInterface.OnClickListener() {  
				public void onClick(DialogInterface dialog, int whichButton) {  
		  
				}  
			});  
		builder.show();  
	} 	

	private void showEmptyPasswordDialog() {  
		AlertDialog.Builder builder = new AlertDialog.Builder(MainScreen.this);  
		builder.setIcon(R.drawable.icon);  
		builder.setTitle("Information");  
		builder.setMessage("You want to use an empty password?");  
		builder.setPositiveButton("Yes",  
			new DialogInterface.OnClickListener() {  
				public void onClick(DialogInterface dialog, int whichButton) {
					LinearLayout gPassword = (LinearLayout)findViewById(R.id.group_password);
					gPassword.setVisibility(View.INVISIBLE);
					LinearLayout gBtns = (LinearLayout)findViewById(R.id.group_btns);
					gBtns.setVisibility(View.VISIBLE);					
				}
			});  
		builder.setNegativeButton("No",  
			new DialogInterface.OnClickListener() {  
				public void onClick(DialogInterface dialog, int whichButton) {  
		  
				}  
			});  
		builder.show();  
	} 	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	    	showExitDialog();
	    	return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	
}